package BS.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.lang3.StringUtils;



import BS.dao.daofactory;
import BS.entity.caozuoyuan;
import BS.entity.gdzc;
import BS.entity.wbw;
import BS.utils.pageinfo;
import BS.utils.pathutils;
import BS.utils.propertiesUtils;


@WebServlet("/wbw")
public class wbwServlet  extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		if("list".equals(method)) {
			this.list(request, response);
		}else if("weihu".equals(method)) {
			this.weihu(request, response);
		}else if("baofei".equals(method)) {
			this.baofei(request, response);
		}else if("waijie".equals(method)) {
			this.waijie(request, response);
		}else if("show".equals(method)) {
			this.show(request, response);
		}else if("update".equals(method)) {
			this.update(request, response);
		}else if("yizhi".equals(method)) {
			this.yizhi(request, response);
		}
	}
	private void weihu(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		String xinghao = request.getParameter("xinghao");
		String date = request.getParameter("date");
		String name = request.getParameter("name");
		String beizhu = request.getParameter("beizhu");
		String num = request.getParameter("num");
		String leixing = request.getParameter("leixing");
		wbw wbw = new wbw();
		wbw.setXinghao(xinghao);
		wbw.setDate(date);
		wbw.setName(name);
		wbw.setBeizhu(beizhu);
		wbw.setNum(num);
		wbw.setLeixing(leixing);
		
		try {
			daofactory.getInstance().getwbwDao().add(wbw);
			response.sendRedirect(pathutils.getBasePath(request) + "wbw?method=list");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private void baofei(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		String xinghao = request.getParameter("xinghao");
		String date = request.getParameter("date");
		String name = request.getParameter("name");
		String beizhu = request.getParameter("beizhu");
		String num = request.getParameter("num");
		String leixing = request.getParameter("leixing");
		wbw wbw = new wbw();
		wbw.setXinghao(xinghao);
		wbw.setDate(date);
		wbw.setName(name);
		wbw.setBeizhu(beizhu);
		wbw.setNum(num);
		wbw.setLeixing(leixing);
		
		try {
			daofactory.getInstance().getwbwDao().add(wbw);
			response.sendRedirect(pathutils.getBasePath(request) + "wbw?method=list");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private void waijie(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		String xinghao = request.getParameter("xinghao");
		String date = request.getParameter("date");
		String name = request.getParameter("name");
		String beizhu = request.getParameter("beizhu");
		String num = request.getParameter("num");
		String leixing = request.getParameter("leixing");
		wbw wbw = new wbw();
		wbw.setXinghao(xinghao);
		wbw.setDate(date);
		wbw.setName(name);
		wbw.setBeizhu(beizhu);
		wbw.setNum(num);
		wbw.setLeixing(leixing);
		
		try {
			daofactory.getInstance().getwbwDao().add(wbw);
			response.sendRedirect(pathutils.getBasePath(request) + "wbw?method=list");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	
	
	
	
	private void list(HttpServletRequest request, HttpServletResponse response) {
		//当前页码
		Integer pageno = getIntParameter(request, "pageno");
		String xinghao = request.getParameter("xinghao");
		
		gdzc gdzc = new gdzc();
		gdzc.setXinghao(xinghao);
		
		//构造了一个pageinfo对象
		pageinfo<gdzc> pageinfo = new pageinfo<>(pageno);
		try {
			pageinfo = daofactory.getInstance().getgdzcDao().list(gdzc,pageinfo);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			request.setAttribute("pageinfo", pageinfo);
			//回写到页面
			request.setAttribute("gdzc", gdzc);
			request.getRequestDispatcher("wbw/wbw.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}       
    }

			
	

	public Integer getIntParameter(HttpServletRequest request,String name) {
		if(StringUtils.isNoneBlank(request.getParameter(name))) {
			return Integer.parseInt(request.getParameter(name));
		}else {
			return null;
		}
	}
	
	private void show(HttpServletRequest request, HttpServletResponse response) {
		//当前页码
		Integer pageno = getIntParameter(request, "pageno");
		String xinghao = request.getParameter("xinghao");
		String name = request.getParameter("name");
		String leixing = request.getParameter("leixing");
		
		
		wbw wbw = new wbw();
		wbw.setXinghao(xinghao);
		wbw.setName(name);
		wbw.setLeixing(leixing);
		
		//构造了一个pageinfo对象
		pageinfo<wbw> pageinfo = new pageinfo<>(pageno);
		try {
			pageinfo = daofactory.getInstance().getwbwDao().list(wbw,pageinfo);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			request.setAttribute("pageinfo", pageinfo);
			//回写到页面
			request.setAttribute("wbw", wbw);
			request.getRequestDispatcher("wbw/list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void update(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		Integer id = getIntParameter(request, "id");
		String xinghao = request.getParameter("xinghao");
		String date = request.getParameter("date");
		String name = request.getParameter("name");
		String beizhu = request.getParameter("beizhu");
		String num = request.getParameter("num");
		String leixing = request.getParameter("leixing");
		String zhuangtai = request.getParameter("zhuangtai");

		
		wbw wbw = new wbw();
		wbw.setXinghao(xinghao);
		wbw.setDate(date);
		wbw.setName(name);
		wbw.setBeizhu(beizhu);
		wbw.setNum(num);
		wbw.setLeixing(leixing);
		wbw.setZhuangtai(zhuangtai);
		
		try {
			daofactory.getInstance().getwbwDao().update(wbw);
			response.sendRedirect(pathutils.getBasePath(request) + "wbw?method=show");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private void yizhi(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String id = request.getParameter("id");
try {
		wbw wbw = daofactory.getInstance().getwbwDao().yizhi(Integer.parseInt(id));
		request.setAttribute("wbw", wbw);
		request.getRequestDispatcher("wbw/yizhi.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
}

	




